LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2014, 03:29 PM   #1
RuijunFan
LQ Newbie
 
Registered: Oct 2014
Posts: 13

Rep: Reputation: Disabled
format '%s' expects type 'char *', but argument 3 has type 'char (*)[31]'


Hello,

My c code complied OK and run well on UNIX.

But on Linux , it reported a warning while compiling .
Code:
 
Typed char varname[31];   // in .h file
 
 
 
Static void get_out( p1,p2,..,varname* bind)
 
{
 
Char tempstr[31];
char outidx[31]; 
.
 
.
 
sprintf(tempstr, ", %s[%s]",bind +i,outidx);   //  compiling warning: format '%s' expects type 'char *', but argument 3 has type 'char (*)[31]'
 
}

Can anybody tell me why ? it is ok in UNIX .

Thanks.

Ray

Last edited by Tinkster; 11-12-2014 at 05:06 PM.
 
Old 11-12-2014, 04:41 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,151

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
To start off with please use code tags!
The warning says what it is the printf statement is expecteing a pointer to a sring of characters ( char* ) where as you are fiving it a pointer to a an array of 31 characters, char* is a zero terminated pointer to a string your array may not be, any way to remove the warning just type cast the pointer ie:
Code:
printf ( "%s\n",(char*)tempstr);
You can also specify the amount of characters to be printed if your array has no 0 terminating character, see the printf man page for full details, passing unterminated strings to printf can cause a segfault
 
Old 11-12-2014, 05:09 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
In addition to Keith's comment.

This doesn't mean that your code is OK in Unix, it means that the compiler you used on Unix sucks.


Cheers,
Tink
 
Old 11-13-2014, 03:44 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You may have altered your code between compilation and publication:

Code:
good:  char chararray[10]; printf ("%s", chararray);
bad:   char chararray[10]; printf ("%s", &chararray);
 
Old 11-13-2014, 04:18 AM   #5
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,151

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Depending on the default settings for the compiler this would still flag up a warning.
 
Old 11-13-2014, 10:23 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Code:
Typed char varname[31];
Huh? Where'd "Typed" come from?
Code:
Char tempstr[31];
Char with a Capital C is not char unless your compiler agrees that it is.
Code:
sprintf(tempstr, ", %s[%s]",bind +i,outidx);
As others have pointed out, you can cast to fix the warning. I recommend you do cast to fix the warning. Personally I enable as many warning options as I can and work towards warning free code. Also said, this doesn't mean the code is correct, but it helps to not be fooling one's self about the results which can occur. At the very least warnings will highlight a situation where the compiler is interpreting something differently than you envisioned and you get a chance to either do a cast or change the code to something else. And I'm assuming that the variable i came from somewhere?

My earlier points are that "Typed" and "Char" are not native (IMHO) C terms, I believe native terms would be "typedef" and "char" (lowercase 'c'). If there are other preprocessor files which give you these abstractions, fine. Just beware of them. And I can't really think of a reason why someone would want to have "Typed" for any real reason. Especially since it only saves you about one keystroke. You have two fewer characters in the name, but you had to shift to type one of those characters. I do get that things like Char are available in the more modern IDEs like Visual Studio or Eclipse. Jus tnot sure yet I agree.
 
Old 11-13-2014, 10:30 AM   #7
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,151

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Quote:
Originally Posted by rtmistler View Post
... Personally I enable as many warning options as I can and work towards warning free code. ...
I do that as well, warnings can be annoying but they are there for a reason, it's always best to get rid of as many as possible ( by fixing the code mostly ).
 
Old 11-13-2014, 12:15 PM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Here's my current flags that I typically use.

-Wall doesn't get them all

Funny, you learn alot about your coding style once you start paying more detailed attention.

And these are by no means the best, they just happen to be useful to me:

Code:
CFLAGS += -Wformat=2 -Werror -Wall -Wextra -Wswitch-default -Wswitch-enum -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wmissing-noreturn
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] .c error when compiling: %lf expects double but 2nd is char. lleb Linux - Software 4 06-07-2013 09:02 AM
How do I write a literal (type char) in C? stf92 Programming 12 07-03-2012 08:02 AM
'C' error in printf warning: format ‘%lf’ expects type ‘double’ ... SuperNomad Programming 4 11-17-2009 09:07 AM
[SOLVED] gcc Warning: Array index has type 'char' traene Programming 4 04-20-2008 08:43 PM
how to covert int to string/char type? nelnel Programming 2 08-23-2005 11:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration